<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
$act = !empty($_GET['act']) ? $_GET['act'] : '';

if($act=="info"){
	
	
	
	$openid = $_GET['openid'];
	
	
	$sql = "SELECT p1.nickname as user_name ,  p1.birthday, p1.sex,p1.mobile_phone,p1.province,p1.city,p1.district,p1.address , p2.nickname ,p2.headimgurl  FROM " . $ecs->table("users") . "AS p1".
			" LEFT JOIN wxch_user AS p2  ON p2.wxid= p1.wxid ".
			" WHERE p1.wxid = '".$openid."' ";
	
	
	$userInfo = $db->getRow($sql);
	
	//var_dump($userInfo);
	
	if($userInfo['user_name']&&$userInfo['mobile_phone']&&$_GET['type']!="edit")
	{
		$smarty->assign('type',$userInfo);
		$smarty->assign('info',$userInfo);
		$smarty->assign('openid',$openid);
		$smarty->display('user.dwt');
		exit;
	}
	
	
	//$smarty->display('user.dwt');
	
	
// 	$userInfo = $db->getRow("SELECT nickname as user_name, birthday, sex,mobile_phone,province,city,district,address FROM " . $ecs->table("users") . " WHERE wxid = '".$openid."' ");
	if($userInfo){
		if($userInfo['birthday']=="0000-00-00"){
			$userInfo['birthday'] = date("Y-m-d");
		}	
	}else{
		$userInfo['birthday'] = date("Y-m-d");
	}
	$date1=explode("-",$userInfo['birthday']);
	$year = $date1[0];
	$month = $date1[1];
	$day = $date1[2];
	
	$province_list = get_regions(1, 1);
	
	if($userInfo['province'] ==0)
	{
		$userInfo['province'] = 6;
	}
	
	if($userInfo['city'] ==0)
	{
		$userInfo['city'] = 76;
	}
	
	
	if($userInfo['district'] ==0)
	{
		$userInfo['district'] = 695;
	}
	
	
	$city_list     = get_regions(2, $userInfo['province']);
	
	$district_list = get_regions(3, $userInfo['city']);
	
	
	
	$smarty->assign('province_list',    $province_list);
	
	$smarty->assign('city_list',        $city_list);
	
	$smarty->assign('district_list',    $district_list);
	
	$smarty->assign('year',$year);
	$smarty->assign('month',$month);
	$smarty->assign('day',$day);
	for($i=$year-150;$i<=$year;$i++){
		$yearArr[$i]=$i;
	}
	for($i=1;$i<=12;$i++){
		$monthArr[$i]=$i;
	}
	for($i=1;$i<=31;$i++){
		$dayArr[$i]=$i;
	}
	
	
	
	
	$smarty->assign('yearArr',$yearArr);
	$smarty->assign('monthArr',$monthArr);
	$smarty->assign('dayArr',$dayArr);
	$smarty->assign('info',$userInfo);
	$smarty->assign('openid',$openid);
	$smarty->display('info.dwt');
}
elseif ($act=="send"){
	
	$key = '29a3712cfff0bacee5a7a593243e6ade';
	$tpl_id = '5971';
	$result=array('error'=>0,'content'=>'发送成功');
	if($_POST['tel']==""){
		$result['error']=1;
		$result['content']='手机不能为空';
		die(json_encode($result));
	}
	if(!preg_match("/^1[3-5,8]{1}[0-9]{9}$/", $_POST['tel'])){
		$result['error']=1;
		$result['content']='手机格式不正确';
		die(json_encode($result));
	}
	
	$wxid = $_POST['wxid'];
	$sql="SELECT wxid FROM ".$ecs->table('users')." WHERE wxid!='".$wxid."' AND  mobile_phone=".$_POST['tel']." LIMIT 1";
	$isWxid = $db->getOne($sql);
	if($isWxid){//存在
		$result ['error'] = '1';
		$result ['content'] = '该手机号码已注册使用！';
		die ( json_encode ( $result ) );
	}
	
	$num=$_POST['tel'];
	$date=local_date('Y年m月d日');
	
	$pattern = '1234567890';
	$rand='';
	for($i = 0; $i < 6; $i ++) {
		$rand .= $pattern {mt_rand ( 0, 9 )}; //生成php随机数
	}
	$_SESSION['vaild_num']=$rand;
	$_SESSION['vaild_time']=intval(time())+120;
	$code = urlencode("#code#=".$rand);
	$URL = "http://v.juhe.cn/sms/send?mobile=". $num."&tpl_id=$tpl_id&tpl_value=".$code."&key=".$key;
	file_get_contents($URL);
	die(json_encode($result));
}
elseif($act == "save"){
	$wxid = $_POST['wxid'];
	$info['nickname'] = isset($_POST['user_name'])?$_POST['user_name']:"";
	$info['birthday'] = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
	$info['sex'] = isset($_POST['sex'])?intval($_POST['sex']):'0';
	$info['mobile_phone'] = isset($_POST['mobile_phone'])?$_POST['mobile_phone']:"";
	$info['province'] = isset($_POST['province'])?$_POST['province']:"";
	$info['city'] = isset($_POST['city'])?$_POST['city']:"";
	$info['district'] = isset($_POST['district'])?$_POST['district']:"";
	$info['address'] = isset($_POST['address'])?$_POST['address']:"";
	$tel_valid = isset($_POST['code'])?$_POST['code']:"";
	if(!preg_match("/^1[3-5,8]{1}[0-9]{9}$/", $_POST['mobile_phone'])){
		$result['error']=1;
		$result['content']='手机格式不正确';
		die(json_encode($result));
	}
	
	$openid = $wxid;
	$userInfo = $db->getRow("SELECT nickname,user_name, birthday, sex,mobile_phone,province,city,district,address FROM " . $ecs->table("users") . " WHERE wxid = '".$openid."' ");
	
	if($userInfo['mobile_phone'] != $info['mobile_phone'])
	{
		
		if (strcasecmp($tel_valid,$_SESSION ['vaild_num'])|| ! $tel_valid || $_SESSION ['vaild_time'] < time () ) {
			$result ['error'] = '1';
			$result ['content'] = '验证码错误';
			die ( json_encode ( $result ) );
		}
	}
	$sql="SELECT wxid FROM ".$ecs->table('users')." WHERE wxid!='".$wxid."' AND  mobile_phone=".$info['mobile_phone']." LIMIT 1";
	$isWxid = $db->getOne($sql);
	if($isWxid){//存在
		$result ['error'] = '1';
		$result ['content'] = '该手机号码已注册使用！';
		die ( json_encode ( $result ) );
	}
	
	foreach($info as $k=>$v){
		$where.=" $k='$v',";
	}
	$where=substr($where,0,strlen($where)-1);
	$sql="UPDATE ".$ecs->table("users")." SET ".$where." WHERE wxid='".$wxid."'";
	$res = $db->query($sql);
	
	if($userInfo['mobile_phone']&&$userInfo['nickname'])
	{
		$result=array('error'=>0,'content'=>'编辑成功！');
	}
	else
	{
		$result=array('error'=>0,'content'=>'恭喜你，会员注册成功！');
		
	}
	
	if(!$res){
		$result['error'] = 1;
		$result['content'] = "编辑失败";
	}
	die(json_encode($result));
}
elseif($act=="user"){
	
	$openid = $_GET['openid'];
	
	$sql = "SELECT p2.nickname ,p2.headimgurl  FROM " . $ecs->table("users") . "AS p1".
				" LEFT JOIN wxch_user AS p2  ON p2.wxid= p1.wxid ".
				" WHERE p1.wxid = '".$openid."' ";
	
	
	
	$userInfo = $db->getRow($sql);
	
	
	
	$smarty->assign('info',$userInfo);
	$smarty->assign('openid',$openid);
	$smarty->display('user.dwt');
	
	
}
